//
//  AppDelegate.swift
//  KawanPrestamo
//
//  Created by Daiyi on 2024/4/16.
//

import UIKit
import DDBase
import KPAppKit
import KPHome
import KPUserCenter
import IQKeyboardManagerSwift


@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        DDAppearance.shared.keyWindow = KPWindow(frame: UIScreen.main.bounds)
        window = DDAppearance.shared.keyWindow
        DDAppearance.shared.defBarColor = .white
        self.loadSetting()
        KPUser.shared.loadUserInfo()
        
        if(KPUser.shared.isLogin){
//        if true {
            // 首页
            self.window?.rootViewController = KPNavigationController(rootViewController: KPHomeController())
        }else {
            // 登录
            let loginNav = KPNavigationController(rootViewController: KPLoginController());
            loginNav.isLogin = true
            self.window?.rootViewController = loginNav;
        }
        
        window?.makeKeyAndVisible()
        
        
        return true
    }

    
    private func loadSetting() {
        DDAppearance.shared.comfirmTitle = "Sí"
        DDAppearance.shared.backIconName = "icon_back"
        DDAppearance.shared.appTinColor = UIColor.rgb(234, 91, 91)
        DDAppearance.shared.emtpy.iconName = "ic_no_data"
        DDAppearance.shared.emtpy.noNeworkIconName = "ic_no_network"
        DDAppearance.shared.emtpy.noNetworkTitle = "No network"
        DDAppearance.shared.emtpy.detailTitle = "No Data"
        DDAppearance.shared.emtpy.titleLabTextColor = UIColor.tin_102
        DDAppearance.shared.network.listKey = "data"
        DDAppearance.shared.network.statusCode = 200
        
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.enableAutoToolbar = true
        IQKeyboardManager.shared.toolbarDoneBarButtonItemText = "Done"
        
        KingfisherManager.shared.cache.memoryStorage.config.totalCostLimit = 64 * 1024 * 1024
        KingfisherManager.shared.cache.memoryStorage.config.countLimit = 1024
        
        DDRouteManager.shared.appScheme = "KawanPrestamo".lowercased()
        DDRouteManager.shared.loadRoutersBlock = { [weak self] in
            guard let self = self else {
                return
            }
            self.loadRouters()
        }
        
        do {
            try DDNetworkManager.shared.setupConfigure(baseUrl: "https://www.inmediatoprestamoapp.com")
            DDNetworkManager.shared.start()
        } catch (let error) {
            self.window?.dd.show(error.localizedDescription)
        }
    }
    
    private func loadRouters() {
        R.KPUCRouter.run
        R.Router.mainRouterRun
        R.KPHRouter.run
    }

}

